package com.xt.website_manager.service.impl;
/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  NewsServiceImpl
 * @Date 2019/8/28/11:06
 * @Description TODO:(一句话描述这个类)
 */

import com.github.pagehelper.PageHelper;
import com.xt.website_manager.dao.NewsDao;
import com.xt.website_manager.dao.UserDao;
import com.xt.website_manager.po.News;
import com.xt.website_manager.po.NewsExt;
import com.xt.website_manager.po.User;
import com.xt.website_manager.service.NewsService;
import com.xt.website_manager.util.MyPageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Resource
    private NewsDao newsDao;
    @Resource
    private UserDao userDao;
    /**
     * 添加新闻
     */
    @Override
    public void addNews(News news) {
        news.setCreateTime(new Date());
        newsDao.addNews(news);
    }
    /**
     *新闻复合查询分页
     * @param newsExt
     */
    @Override
    public MyPageInfo<NewsExt> findNewsList(NewsExt newsExt) {
        if(newsExt.getPageNow()==null || newsExt.getPageNow()<1){
            newsExt.setPageNow(1);
        }

        if(newsExt.getCsid()!=null && newsExt.getCsid()==-1){
            newsExt.setCsid(null);
        }
        PageHelper.startPage(newsExt.getPageNow(),8);
        //查询新闻结果集
        List<NewsExt> allNews = newsDao.findNewsList(newsExt);

        //返回新闻结果集，allNews：新闻结果集   navigatePages =5  设置页码数为5
        MyPageInfo<NewsExt> pageInfo = new MyPageInfo<NewsExt>(allNews,7);

        pageInfo.setSearchCsid(newsExt.getCsid());
        pageInfo.setTitle(newsExt.getTitle());
        return pageInfo;
    }

    /**
     * 删除新闻
     * @param id
     */
    @Override
    public void delNews(Integer id) {
        newsDao.delNews(id);
    }
    /**
     * 根据id查询新闻信息
     * @param nid
     * @return
     */
    @Override
    public News findNewsById(Integer nid) {
        return newsDao.findNewsByID(nid);
    }

    /**
     * 修改新闻
     * @param newsExt
     */
    @Override
    public void updateNews(NewsExt newsExt) {
        newsDao.updateNews(newsExt);
    }

    /**
     * 根据二级类目id获取新闻总数
     * @param csid
     * @return
     */
    @Override
    public Integer getNewsCount(Integer csid) {
        return newsDao.getNewsCount(csid);
    }

    /**
     *  根据uid查询新闻
     */
    @Override
    public News findNewsByUid(Integer uid) {
        return newsDao.findNewsByUid(uid);
    }
}
